package com.ponxu.log;

import java.lang.reflect.Constructor;

import com.ponxu.config.Properties;
import com.ponxu.log.loggers.NoOpLog;

public class LogFactory {
	private static Class<?> logClass;
	private static Constructor<?> logConstructor;
	static {
		try {
			Properties p = new Properties();
			p.load(LogFactory.class.getResourceAsStream("/log.properties"));
			String className = p.getProperty("log");

			logClass = Class.forName(className);
			if (logClass == null || logClass.getSuperclass() != Log.class) {
				logClass = NoOpLog.class;
			}
		} catch (Exception e) {
			logClass = NoOpLog.class;
		}
	}

	public static Log getLog(String name) {
		Log log = null;
		try {
			if (logConstructor == null) {
				logConstructor = logClass.getConstructor(String.class);
			}
			log = (Log) logConstructor.newInstance(name);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return log;
	}

	public static Log getLog(Class<?> clazz) {
		return getLog(clazz.getName());
	}

	public static Log getLog() {
		return getLog(Thread.currentThread().getStackTrace()[2].getClassName());
	}
}
